<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>WebSocket Chat</title>
</head>
<style>
    form {
        width: 406px;
        height: 650px;
        border: 4px solid #98bcde;
        border-radius: 10px;
        margin: 0 auto;
        background-color: #eceff9;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;

    }
    h3 {
        color: #92acdc;
        text-align: center;
        font-size: 26px;
    }
    textarea {
        resize: none;
        font-size: 20px;
        width: 401px;
        height: 511px;
    }
    .msg {
        width: 324px;
        height: 40px;
        text-indent: 10px;
        font-size: 20px;
        outline: none;
    }
    .btn {
        width: 140px;
        height: 46px;
        background-color: #d8f1f9;
        border-radius: 6px;
        border: 1px solid #98bcde;
        font-size: 18px;
        color: #92acdc;
        font-weight: bold;
    }

</style>
<body>
<script type="text/javascript">
    var socket;
    //心跳
    var heartCheck = {
        timeout: 30000, //60ms
        timeoutObj: null,
        serverTimeoutObj: null,
        reset: function() {
            clearTimeout(this.timeoutObj);
            clearTimeout(this.serverTimeoutObj);
            this.start();
        },
        start: function() {
            var self = this;
            this.timeoutObj = setTimeout(function() {
                send();
                self.serverTimeoutObj = setTimeout(function() {
                    socket.close(); //如果onclose会执行reconnect，我们执行socket.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
                }, self.timeout)
            }, this.timeout)
        },
    }

    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (window.WebSocket) {
        /*这里必须实际ip*/
        socket = new WebSocket("ws://localhost:6789/ws");
        heartCheck.start();

        //这里好像是服务器返回消息
        socket.onmessage = function(event) {
            var ta = document.getElementById('responseText');
            if(event.data != "ping"){//ping是心跳
                ta.value = ta.value + '\n' + event.data
            }
            heartCheck.reset();
        };
        socket.onopen = function(event) {
            var ta = document.getElementById('responseText');
            ta.value = "连接开启!";
        };
        socket.onclose = function(event) {
            var ta = document.getElementById('responseText');
            ta.value = ta.value + "连接被关闭";
        };
    } else {
        alert("你的浏览器不支持 WebSocket！");
    }

    /**
     * 发送消息
     * @param value
     */
    function send(value) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            var message = {
                type: "chat90"
            }
            socket.send(JSON.stringify(message));
        } else {
            alert("连接没有开启.");
        }
    }


    window.onbeforeunload = function(event) {
        event.returnValue = "刷新提醒";
    };
</script>
<form onsubmit="return false;">
    <h3>InChat 聊天室-8070</h3>
    <textarea id="responseText"></textarea>
    <br>
    <input class='msg' type="text" name="message" placeholder='发送消息' value="">
    <input class='btn' type="button" value="发送" onclick="send(this.form.message.value)">
</form>
<br>
<br>
</body>
</html>